import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-15
 * Time: 21:25
 */
public class test1 {
    /*
    LeetCode 49 字母异位词分组
     */

    // 排序：对每一个单词进行排序之后把相同的放入哈希表中
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<String,List<String>>();
        for(String str :strs){
            // 将每个单词进行排序
            char[] array = str.toCharArray();
            Arrays.sort(array);
            // 将排序好的每个单词设为key
            String key = new String(array);
            List<String> list = map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
